package com.yaozheng.vocationaltraining.widget;

import android.R.color;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.widget.ProgressBar;

public class TextProgressBar extends ProgressBar {
	private String str;
	private Paint mPaint;

	public TextProgressBar(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
		initText();
	}

	public TextProgressBar(Context context, AttributeSet attrs) {
		super(context, attrs);
		initText();
	}

	public TextProgressBar(Context context) {
		super(context);
		initText();
	}

	@Override
	protected synchronized void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		Rect rect = new Rect();
		this.mPaint.getTextBounds(this.str, 0, this.str.length(), rect);
		int x = (getWidth() / 2) - rect.centerX();// 让现实的字体处于中心位置;;
		int y = (getHeight() / 2) - rect.centerY();// 让显示的字体处于中心位置;;
		canvas.drawText(this.str, x, y, this.mPaint);
	}

	@Override
	public void setProgress(int progress) {
		super.setProgress(progress);
	}

	public void setProgressText(int progress, String textStr) {
		this.str = textStr;
		super.setProgress(progress);

	}

	// 初始化，画笔
	private void initText() {
		this.mPaint = new Paint();
		this.mPaint.setAntiAlias(true);// 设置抗锯齿;;;;
		this.mPaint.setColor(Color.WHITE);
		float mTextSize = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,
				16, getResources().getDisplayMetrics());
		this.mPaint.setTextSize(mTextSize);
	}

}
